<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addRole"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4, 5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px" :data="companyMessage">
              <el-form-item label="公司名称">
                <el-input v-model="companyMessage.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyMessage.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyMessage.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyMessage.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

      </el-card>
    </div>
    <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
      <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入角色描述" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetRoleList, reqDelRole, reqAddRole, reqGetRoleById } from '@/api/setting'
import { reqGetCompanyById } from '@/api/company'
export default {
  name: 'Setting',
  data() {
    return {
      companyMessage: {},
      loading: false,
      list: [],
      page: 1,
      pagesize: 5,
      total: 0,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '新增角色'
    }
  },
  async created() {
    this.getRoleList()
    this.getCompanyById()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const { data: { data: { rows, total }}} = await reqGetRoleList(this.page, this.pagesize)
      // console.log(data)
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      // 每页条数改变，重新发送请求
      this.pagesize = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getRoleList()
    },
    delRole(id) {
      this.$confirm('是否删除该角色', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await reqDelRole(id)
        this.$message.success('删除角色成功')
        if (this.list.length === 1 && this.page > 1) {
          this.page = this.page - 1
        }
        this.getRoleList()
      }).catch(() => {
        this.$message.info('已取消删除该角色')
      })
    },
    addRole() {
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    submit() {
      this.$refs.roleForm.validate(async(boo) => {
        if (boo) {
          console.log('校验通过')
          await reqAddRole(this.form)
          this.$message.success('操作成功')
          this.getRoleList()
          this.closeDialog()
        } else {
          console.log('不通过')
        }
      })
    },
    async editRole(id) {
      this.showDialog = true
      const { data: { data }} = await reqGetRoleById(id)
      this.form = data
    },
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + 1 + index
    },
    async getCompanyById() {
      const { data: { data }} = await reqGetCompanyById(this.form.id)
      console.log(data)
      this.companyMessage = data
    }
  }
}
</script>

<style>

</style>

